package com.mj.action.system;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mj.base.BaseController;
import com.mj.model.system.Role;
import com.mj.service.system.MenuService;
import com.mj.service.system.RoleService;
import com.mj.service.system.UserService;

/**
 * 角色管理
 *@ClassName:RoleAction.java
 *@author dsc
 *@date 2017年7月22日
 *@Description TODO
 *
 */
@Controller
@RequestMapping("role")
public class RoleAction extends BaseController{
	@Resource
	private RoleService roleService;
	@Resource
	private MenuService menuService;
	@Resource
	private UserService userService;
	
	/**
	 * 角色列表页面，
	 * @param m
	 * @return
	 */
	@RequestMapping(value = "")
	public String toRole(Model model){
		 return "system/role/role_list"; 
	}
	
	  //菜单树形结构  
  

	/**
	 * 角色列表初始化方法
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="rolelist")
	 public String rlist(HttpServletRequest request) {
		String roleName = request.getParameter("roleName");
		List<Role> rolelist = roleService.roleBindMenuName(roleName);
		return renderJsonArray(rolelist); 
	 }
	
	
	
	/**
	 * 添加角色
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="addRole")
	public Boolean addRole(Role role){
		int flag = roleService.addRole(role);
		if(flag > 0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 修改角色
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="editRole")
	public Boolean editRole(Role role){
		int flag = roleService.updateById(role);
		if(flag > 0){
			return true;
		}else{
			return false;
		}
	}
	
	
	/**
	 * 查询角色
	 */
	@ResponseBody
	@RequestMapping(value="getRoleById")
	public String getRoleById(){
		Role u = roleService.selectById((Integer.valueOf(request.getParameter("uid"))));
		return renderJson(u);
	}
	
	/**
	 * 判断角色名是否存在
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="isExistsRole")
	public String ExistsRole(){
		
		return "";
	}
	
	/**
	 * 角色菜单、按钮权限设置
	 * @return
	 */
	@RequestMapping(value="perSet")
	public String perSet(){
		Role u = roleService.selectById(Integer.valueOf(request.getParameter("rid")));
		if(u.getButtonsIds() == null){
			u.setButtonsIds("");
		}
		if(u.getMenuIds() == null){
			u.setMenuIds("");
		}
		request.setAttribute("roleInfo", u);
		return "system/role/role_permission";
	}
	
	/**
	 * 保存角色菜单、按钮权限
	 * @param str
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "savePermission", method = RequestMethod.POST)
	@ResponseBody
	public String savPermission(String btnArr, String menuArr,String roleId) {
		String result;
		try {
			Role role = new Role();
			role.setMenuIds(menuArr);
			role.setButtonsIds(btnArr);
			role.setRoleId(Integer.valueOf(roleId));
			roleService.updateById(role);
			result = "{'result':true}";
		} catch (Exception e) {
			e.printStackTrace();
			result = "{'result':false,'msg':'保存失败'}";
		}
		return result;
	}
	
}
